package com.twd.netty;

import com.twd.handler.HelloNettyServerHandler;
import com.twd.handler.HttpRequestHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.DefaultEventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;


public class MyNettyServerTCP1 {

    private static final Integer PORT = 8080;

    public static void main(String[] args) throws InterruptedException {
        //设置线程组
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            //启动引导
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .handler(new ChannelInboundHandlerAdapter(){
                        @Override
                        public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
                            System.out.println("handlerAdded");                        }

                        @Override
                        public void channelActive(ChannelHandlerContext ctx) throws Exception {
                            System.out.println("channelActive");
                        }

                        @Override
                        public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
                            System.out.println("channelRegistered");
                        }
                    })
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            System.out.println("添加自处理器");
                            ChannelPipeline pipeline = socketChannel.pipeline();
                            //加入自定义的handler处理器
                            socketChannel.pipeline().addLast(new DefaultEventLoopGroup(),new StringDecoder(CharsetUtil.UTF_8));
                            socketChannel.pipeline().addLast(new DefaultEventLoopGroup(),new StringEncoder(CharsetUtil.UTF_8));
                            pipeline.addLast(new DefaultEventLoopGroup(),new HttpRequestHandler());

                            pipeline.addLast(new DefaultEventLoopGroup(),new HelloNettyServerHandler());
                        }
                    });

            //绑定端口号
            ChannelFuture future = bootstrap.bind(PORT+1).sync();

            //等待服务端监听端口关闭
            future.channel().closeFuture().sync();
        } finally {
            //关闭资源
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }

    }
}